<?php
require_once '../libs/app.config.php';
require_once INCLUDE_PATH . 'template.config.php';
require_once 'head.php';
$charset ? null : $charset = 'cn';

$cat_name ? null : $cat_name = 'EXPRESS TRACING';
$cat_name = $tpm->escape ( $cat_name );
$charset = $tpm->escape ( $charset );
$tpl->assign ( "cat_name", $cat_name );
$src = $$cat_name;
$src = $src ['content'];
$tpl->assign ( "src", $src );

if ($_POST || $id!=null) {
	$tracingNo = $_POST ['code'];
	$new_string = ereg_replace("[^A-Za-z0-9]", ",", $tracingNo );
	$temp = explode ( ',,,,', $new_string );
	$arr = implode ( ',', $temp );
	$temp = explode ( ',,,', $arr );
	$arr = implode ( ',', $temp );
	$temp = explode ( ',,', $arr );
	$final = implode ( ',', $temp );
	$array = explode ( ',', $final );
	$size = count ( $array );
	
	if ($size > 1) {
		for($j = 0; $j < $size; $j ++) {
			if($array[$j]!="")
			{
			$sql = "select* from orders where orderId='" . $array [$j] . "'";
			$sql2 = "select DATE_FORMAT(max(time), '%Y-%m-%d') as time from order_records where orderId='" . $array [$j] . "'";
			$query = $tpm->query ( $sql );
			$query2 = $tpm->query($sql2);
			$query3=$tpm -> num_rows($tpm->query($sql));
			$rows = $tpm->fetch_array ( $query );
			$row = $tpm->fetch_array ( $query2 );
			
			if($query3==0)
			{
				$temp = "不存在";
				$orderList [] = array ('orderId'=>$array[$j],
						'status' => $temp
						);
			}else{
				$orderList [] = array (
					'orderId' => $rows ['orderId'],
					'time' => $row['time'],
					'destination' => $rows ['destination'],
					'items' => $rows ['items'],
					'status' => $rows ['status']
			);
			}
			}
		}
		$tpl->assign("orderList",$orderList);
		$tpl->display ( "tracing_list_cn.html" );
	
	} else {
		$tpl->assign ( "tracingNo", $tracingNo );
		if($tracingNo != null)
			$id = $tracingNo;
		
		if ($id) {
			$tpl->assign ( "id", $id );
			$sql = "select distinct r.orderId,  DATE_FORMAT(r.time, '%Y-%m-%d') as time, r.location, r.detail from order_records r, orders o  where  r.orderId='" . $id . "' order by time asc";
			$query = $tpm->query ( $sql );
			$totals = $tpm->num_rows ( $query );
			$tpn = new Page ();
			
			$sql2 = "select *,DATE_FORMAT(o.completeTime, '%Y-%m-%d') as completeTime from orders o where o.orderId='" . $id . "'";
			$query = $tpm->query ( $sql2 );
			if ($tpm->affected_rows ( $query )) {
				$latest = $tpm->fetch_array ( $query );
				$tpl->assign ( "latest", $latest );
			}
		} else {
			$totals = null;
			$tpn = new Page ();
			$latest = null;
			$tpl->assign ( "latest", $latest );
		}
		
		// print_r($latest);
		
		if ($charset == 'en') {
			$tpn->pageft ( $totals, 10 );
			$sql = $sql . " limit " . $tpn->getfirstcount () . ",10";
			$query = $tpm->query ( $sql );
			while ( $row = $tpm->fetch_array ( $query ) )
				$list [] = $row;
			$tpl->assign ( "list", $list );
			$tpl->assign ( "page_nav", $tpn->getpagenav () );
			$tpl->display ( "latest_en.html" );
		} else {
			$tpn->pageft_cn ( $totals, 20 );
			$sql = $sql . " limit " . $tpn->getfirstcount () . ",20";
			$query = $tpm->query ( $sql );
			while ( $row = $tpm->fetch_array ( $query ) )
				$list [] = $row;
			$tpl->assign ( "list", $list );
			$tpl->assign ( "page_nav", $tpn->getpagenav () );
			$tpl->display ( "tracing_single_cn.html" );
		}
	}
} else
	$tpl->display ( "tracing_cn.html" );
?>